<template>
  <div>
    <page-tools>
      <template v-slot:before>
        <el-row type="flex" justify="space-between" align="middle">
          <el-col>
            <el-row type="flex" justify="space-around" align="middle">
              <h5>学科名称</h5>
              <el-input ref="clearInput" v-model="input" placeholder="" clearable />
              <el-button size="small" @click="clear">清除</el-button>
              <el-button size="small" type="primary" @click="search">搜索</el-button>
            </el-row>
          </el-col>
        </el-row>
      </template>
      <template v-slot:after>

        <el-button size="small" type="success" @click="showDialog(0)">
          <i class="el-icon-edit" style="color:#fff" />
          <span>新增学科</span>
        </el-button>
      </template>

    </page-tools>
    <i class="el-icon-info">数据一共{{ page.counts }}条</i>
    <!-- 表格 row-key 展开树形-->
    <el-table border :data="list" row-key="id">
      <el-table-column label="序号" type="index" />
      <el-table-column label="学科名称" prop="subjectName" />
      <el-table-column label="创建者" prop="creatorID" />
      <el-table-column label="创建日期" prop="addDate">
        <template slot-scope="{row}">
          {{ row.addDate | formatDate }}
        </template>
      </el-table-column>
      <el-table-column :formatter="showFormat" label="前台是否显示" prop="isFrontDisplay" />
      <el-table-column label="二级目录" prop="twoLevelDirectory" />
      <el-table-column label="标签" prop="tags" />
      <el-table-column label="题目数量" prop="totals" />
      <el-table-column label="操作" width="250px">
        <template slot-scope="{row}">
          <el-button type="text" @click="classification">学科分类</el-button>
          <el-button type="text" @click="classLabel">学科标签</el-button>
          <el-button type="text" @click="editBtn(1,row.id)">修改</el-button>
          <el-button type="text" @click="delRoot(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-row type="flex" justify="end" align="middle" style="height: 60px">
      <el-pagination
        :current-page="page.page"
        :page-sizes="[5, 10, 15]"
        :page-size="page.pagesize"
        layout=" prev ,pager,next ,sizes,jumper"
        :total="page.counts"
        @current-change="CurrentChange"
        @size-change="handleSizeChange"
      />
    </el-row>
    <!-- 分页器 -->
    <!-- 新增弹框 -->
    <el-dialog
      :title=" changeTitle==0?'新增学科':'修改学科'"
      :visible="dialogVisible"
      width="30%"
      @close="btnCancel"
    >
      <!-- 表单 -->
      <el-form ref="myForm" :model="formList" label-width="120px">
        <el-form-item label="学科名称" prop="name">
          <el-input v-model="formList.subjectName" style="width:270px" placeholder="请输入学科名称" />
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="formList.isFrontDisplay"
            active-value="0"
            inactive-value="1"
          />
        </el-form-item>
      </el-form>
      <!-- 表单 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="btnCancel">取 消</el-button>

        <el-button type="primary" @click="btnOK">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增弹框 -->
    =======
    <el-card>
      <!-- header -->
      <el-row type="flex" justify="space-between" style="margin-bottom: 20px">
        <el-col :span="21">
          <el-input
            size="mini"
            placeholder="请输入学科名"
            style="width: 300px; margin-right: 20px"
          />
          <template>
            <el-button size="mini">清除</el-button>
            <el-button size="mini" type="primary">搜索</el-button>
          </template>
        </el-col>
        <el-col :span="3">
          <el-button
            type="success"
            icon="el-icon-plus"
            size="mini"
          >新增学科</el-button>
        </el-col>
      </el-row>

      <!-- info -->
      <el-alert title="数据一共21条" :closable="false" show-icon type="info" />

      <!-- 表格 -->
      <el-table style="width: 100%; margin-top: 20px">
        <el-table-column align="center" label="序号" type="index" width="width" />
        <el-table-column align="center" label="学科名称" width="width" />
        <el-table-column align="center" label="创建者" width="width" />
        <el-table-column align="center" label="创建日期" width="width" />
        <el-table-column align="center" label="前台是否显示" width="width" />
        <el-table-column align="center" label="二级目录" width="width" />
        <el-table-column align="center" label="标签" width="width" />
        <el-table-column align="center" label="题目数量" width="width" />
        <el-table-column align="center" label="操作" width="width">
          <template>
            <el-button type="text">学科分类</el-button>
            <el-button type="text">学科标签</el-button>
            <el-button type="text">修改</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import PageTools from '@/components/pageTools'
import { getSubject, addSubject, updateSubject, getSubjectDetail, removeSubject } from '@/api/subject'
export default {
  name: '',
  components: {
    PageTools

  },
  props: {

  },
  data() {
    return {
      list: [],
      // rules: {
      //   name: [
      //     { required: true, message: '请输入活动名称', trigger: 'blur' },
      //     { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
      //   ]
      // },
      input: '',
      page: {
        page: 1,
        pagesize: 5,
        counts: 0
      },
      formList: {
        addDate: '',
        creatorID: '',
        id: '',
        isFrontDisplay: '',
        subjectName: '',
        tags: '',
        totals: '',
        twoLevelDirectory: '',
        username: ''
      },
      dialogVisible: false,
      changeTitle: 0
    }
  },
  computed: {

  },
  watch: {

  },
  created() {
    this.getSubject()
  },
  mounted() {

  },
  methods: {
    classification() {
      this.$store.commit('subject/setShowlink', true)
      this.$router.push({ path: 'catalogue' })
    },
    classLabel() {
      this.$store.commit('subject/setShowlink', true)
      this.$router.push({ path: 'label' })
    },
    // 展示弹框
    showDialog(can) {
      this.changeTitle = can
      this.dialogVisible = true
    },
    // 取消
    btnCancel() {
      this.dialogVisible = false
      this.$refs.myForm.resetFields()
    },
    // 确定
    async  btnOK() {
      if (this.formList.id) {
        // 编辑通过id判断
        await updateSubject(this.formList)
      } else {
        // 新增
        await addSubject(this.formList)
      }

      this.$message.success('操作权限成功')
      this.getSubject()
      this.dialogVisible = false
    },
    // 修改
    async editBtn(can, id) {
      this.changeTitle = can
      this.dialogVisible = true
      console.log(id)
      const res = await getSubjectDetail(id)
      console.log(res, '11111111111')
      this.formList = res
    },
    // 删除
    delRoot(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await removeSubject(id)
        this.$message.success('删除成功')
        this.getSubject()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 获取学科简单列表
    async getSubject() {
      const res = await getSubject(this.page)
      this.list = res.items
      this.page.counts = res.counts
    },
    //  格式表格内容
    showFormat(row, column) {
      switch (row.isFrontDisplay) {
        case 0:
          return '是'
          // break
        case 1:
          return '否'
          // break
        default:
          return '未知'
      }
    },
    // 分页器
    CurrentChange(num) {
      this.page.page = num
      this.getSubject(this.page)
    },
    handleSizeChange(num) {
      this.page.pagesize = num
      this.getSubject(this.page)
    },
    clear() {
      this.$refs.clearInput.value = ''
    },
    search() {
      const res = this.list.filter((item) => {
        return item.subjectName === this.$refs.clearInput.value
      })
      this.list = res
    }
  }
}
</script>

<style scoped lang='scss'>
h5{
  width: 60px;
}
.el-button--small{
 margin-left: 10px;
}
::v-deep .el-input__inner{
  width: 200px;
  height: 32px ;
  line-height: 32px;
  padding:0 20px;
}
::v-deep .el-input{
 width: 200px;
}
    i {
      margin-right: 5px;
      color: #999;
    }
   ::v-deep .el-pagination .el-select .el-input .el-input__inner{
      width: 120px;
        height:28px;
    }
   ::v-deep .el-pagination__editor.el-input .el-input__inner {
        width: 120px;
    }
::v-deep .el-dialog__header{
  display: flex;
  align-items: center;

 background-color:rgb(64, 158, 255)
}
 ::v-deep  .el-dialog__headerbtn .el-dialog__close{
   color: white;
}
 ::v-deep  .el-dialog__title{
   color: white;
}
</style>

